package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.CustomEvent;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.activity.ComicDirectorySelectorActivity;
import com.qire.manhua.databinding.ActivityComicDirectorySelectorBinding;
import com.qire.manhua.databinding.LayoutComicDirectorySelectorItemBinding;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.impl.ComicDirectorySelectorViewModel;
import com.qire.manhua.model.viewModel.impl.ComicDirectorySelectorViewModel.DirectorySelectorItemViewModel;
import com.qire.other.recorder.ComicActionRecorder;

import androidx.annotation.NonNull;

public class ComicDirectorySelectorController extends EventController<ComicDirectorySelectorActivity> implements AutoVariable<LayoutComicDirectorySelectorItemBinding> {

    private ComicDirectorySelectorViewModel viewModel;

    public ComicDirectorySelectorController(@NonNull ComicDirectorySelectorActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityComicDirectorySelectorBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setComicDirectorySelectorController(this);
    }

    @Override
    public void bindVariable(LayoutComicDirectorySelectorItemBinding viewBind) {
        viewBind.setComicDirectorySelectorController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ComicDirectorySelectorViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void backBtnOnClick(View view){
        owner.onBackPressed();
    }

    public void sortBtnOnClick(View view){
        viewModel.toggleSortModeText();
    }

    public void downloadBtnOnClick(View view){
        ComicBookEntity comicBookEntity = viewModel.comicBookEntity();
        if(ObjectUtil.isNull(comicBookEntity)) {
            ToastHelper.showToast("没有漫画数据");
            owner.onBackPressed();
            return;
        }

        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDownloadSelectorPage)
                .withData("comicBookEntity", comicBookEntity)
                .navigation();

        // 行为数据统计
        ComicActionRecorder.build(ComicActionRecorder.count_down)
                .setSource("目录页")
                .setComicInfo(comicBookEntity.id, comicBookEntity.bookName)
                .doRecorde(view.getContext());
    }

    public void directorySelectorOnItemClick(View view){
        DirectorySelectorItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), DirectorySelectorItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            NoticeEventBus.NOTICE.sendEvent(CustomEvent.SELECT_CHAPTER, itemViewModel.directoryInfo());
            owner.onBackPressed();
        }
    }

}
